package com.sgb.goods.viewmodel.publish;

import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.AlbumEntity;
import com.sgb.goods.entity.BrandEntity;
import com.sgb.goods.entity.MenuEntity1;
import com.sgb.goods.entity.ProListEntity;
import com.sgb.goods.entity.StorageManEntity;
import com.sgb.goods.entity.UnitEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.publish.pop.BrandPopWindow;
import com.sgb.goods.view.adapter.publish.pop.FuturesPopWindow;
import com.sgb.goods.view.adapter.publish.pop.GoodsTypePopWindow;
import com.sgb.goods.view.adapter.publish.pop.SelectAlbumPopWindow;
import com.sgb.goods.view.adapter.publish.pop.StoragePopWindow;
import com.sgb.goods.view.adapter.publish.pop.UnitPopWindow;
import com.sgb.goods.view.adapter.publish.pop.VideoSelectPopWindow;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.BasePopWindow;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 弹窗相关的viewModel
 * @author cyj
 */
public class PublishPopViewModel2 extends BaseViewModel {

    //商品分类弹窗
    public GoodsTypePopWindow goodsTypePopWindow;
    //视频列表弹窗
    public VideoSelectPopWindow videoListSelectPopWindow;
    //相册、网络相册选择弹窗
    public SelectAlbumPopWindow selectAlbumPopWindow;
    //仓储弹窗
    public StoragePopWindow storagePopWindow;
    //品牌弹窗
    public BrandPopWindow brandPopWindow;
    //单位弹窗
    public UnitPopWindow unitPopWindow;
    //货期弹窗
    public FuturesPopWindow futuresPopWindow;

    public PublishPopViewModel2() {

    }

    //------------------------------商品类型弹窗-------------------------------------
    /** 获取商品类型弹窗 */
    public GoodsTypePopWindow getGoodsTypePopWindow() {
        if (goodsTypePopWindow == null) {
            goodsTypePopWindow = new GoodsTypePopWindow(GoodsSdk.getInstance().getTopActivity());
        }
        return goodsTypePopWindow;
    }

    /** 获取商品分类 */
    public void getGoodsCategory(View view) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().goodsCategory1().enqueue(new GoodsCallBack<BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity1<MenuEntity1<MenuEntity1>>>> data) {
                // 商品分类数据弹窗
                getGoodsTypePopWindow().setData(data.getData());
                getGoodsTypePopWindow().showAsDropDown(view);
            }
        });
    }

    //------------------------------视频选择弹窗-------------------------------------
    /** 初始化视频选择弹窗 */
    public VideoSelectPopWindow getVideoSelectPopWindow() {
        //-------------------------视频列表选择弹窗----------------------------------------------
        if (videoListSelectPopWindow == null) {
            videoListSelectPopWindow = new VideoSelectPopWindow(GoodsSdk.getInstance().getTopActivity());
            videoListSelectPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            videoListSelectPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        }
        return videoListSelectPopWindow;
    }

    //------------------------------相册、网络相册选择弹窗-------------------------------------
    /** 获取相册、网络相册选择弹窗 */
    public SelectAlbumPopWindow getSelectAlbumPopWindow(View view) {
        if (selectAlbumPopWindow == null) {
            selectAlbumPopWindow = new SelectAlbumPopWindow(GoodsSdk.getInstance().getTopActivity());
        }
        return selectAlbumPopWindow;
    }

    /** 获取相册//2021/1/18 更改为用cid3作为请求参数 */
    public void getSupplierAlbumByCName(View view, String cid) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getSupplierAlbumByCName(cid).enqueue(new GoodsCallBack<BaseEntity<List<AlbumEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AlbumEntity>> data) {
                // 商品分类数据弹窗
                getSelectAlbumPopWindow(view).setData(data.getData());
                getSelectAlbumPopWindow(view).showAsDropDown(view);
            }
        });
    }

    //------------------------------仓储选择弹窗-------------------------------------
    private int storagePage = 1;

    /** 获取仓储选择弹窗 */
    public StoragePopWindow getStoragePopWindow(View view) {
        if(storagePopWindow == null) {
            storagePage = 1;
            storagePopWindow = new StoragePopWindow(GoodsSdk.getInstance().getTopActivity());
            storagePopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            storagePopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            storagePopWindow.inputTextInterface(s -> getStorageList(view, false));
            storagePopWindow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                    getStorageList(view, true);
                }

                @Override
                public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                    getStorageList(view, false);
                }
            });
        }
        return storagePopWindow;
    }

    /**
     * 仓储列表
     * 注意：
     * 1.此处选择仓储的业务场景次数较少，因此只做页数缓存，不做数据缓存
     * 2.若是数据21条，每页查询20条，当前选中第二页的第一条，也就是21的位置，那么下次进入由于isLodMore=false则
     * 会是查询第一页不会选中任何一条，当用户手动加载第二页，那么会自动选中第21条，对于关闭弹窗等取消操作也是满足的
     * @param view    弹窗参考左上角的显示大小位置
     * @param isLodMore    是否加载更多
     */
    public void getStorageList(View view, boolean isLodMore) {
        storagePage = isLodMore ? storagePage + 1 : 1;
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getStorageList(storagePage, Constants.PAGE_SIZE_20,
                getStoragePopWindow(view).contentText.get()).enqueue(new GoodsCallBack<BaseEntity<ProListEntity<List<StorageManEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<StorageManEntity>>> data) {
                // 仓储列表弹窗
                getStoragePopWindow(view).setData(data.getData().getList(), isLodMore);
                getStoragePopWindow(view).showAsDropDown(view);
            }

            @Override
            public void onFail() {
                getStoragePopWindow(view).setData(null, isLodMore);
            }
        });
    }

    //------------------------------品牌选择弹窗-------------------------------------
    /** 获取品牌选择弹窗 */
    public BrandPopWindow getBrandPopWindow(View view) {
        if(brandPopWindow == null) {
            brandPopWindow = new BrandPopWindow(GoodsSdk.getInstance().getTopActivity());
            brandPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            brandPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            brandPopWindow.inputTextInterface(v -> getBrandList(view, v));
        }
        return brandPopWindow;
    }

    /** 品牌列表 */
    public void getBrandList(View view, String id) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().brandList(id, getBrandPopWindow(view).contentText.get())
                .enqueue(new GoodsCallBack<BaseEntity<List<BrandEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<BrandEntity>> data) {
                getBrandPopWindow(view).setData(data.getData(), id);
                getBrandPopWindow(view).showAsDropDown(view);
            }
        });
    }

    //------------------------------单位选择弹窗-------------------------------------
    /** 获取单位选择弹窗 */
    public UnitPopWindow getUnitPopWindow(View view) {
        if(unitPopWindow == null) {
            unitPopWindow = new UnitPopWindow(GoodsSdk.getInstance().getTopActivity());
            unitPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            unitPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
            unitPopWindow.inputTextInterface(v -> getUnitList(view));
        }
        return unitPopWindow;
    }

    /** 获取价格单位 */
    public void getUnitList(View view) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().unitList(getUnitPopWindow(view).contentText.get()).enqueue(new GoodsCallBack<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<UnitEntity>> data) {
                getUnitPopWindow(view).setData(data.getData());
                getUnitPopWindow(view).showAsDropDown(view);
            }
        });
    }

    //------------------------------货期选择弹窗-------------------------------------
    /** 获取货期选择弹窗 */
    public FuturesPopWindow getFuturesPopWindow(View view) {
        if(futuresPopWindow == null) {
            futuresPopWindow = new FuturesPopWindow(GoodsSdk.getInstance().getTopActivity());
            futuresPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            futuresPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        }
        return futuresPopWindow;
    }

    @Override
    public void onDestroy(@NonNull @NotNull LifecycleOwner owner) {
        // 手动释放弹窗资源
        goodsTypePopWindow = null;
        selectAlbumPopWindow = null;
        storagePopWindow = null;
        brandPopWindow = null;
        unitPopWindow = null;
        futuresPopWindow = null;
    }
}
